# encoding: utf-8
# @File  : page_user_info.py
# @Author: wu shaofan
# @Date  :  2024/04/17
# @Desc : 登录成功后跳转到个人中心页面
import datetime
import random
from selenium.webdriver.common.by import By
from PO.base.base import Base


class PageUserinfo(Base):
    # TODO ****************以下为个人中心页面元素配置信息*******************
    # 登录账号安全文本
    user_home_user_name = By.CSS_SELECTOR, 'div[class="mu-midd fl"] span'
    # 返回商城首页按钮
    user_home_back_home_btn = By.CSS_SELECTOR, 'div[class="m-index"] a[class="home"]'
    # 个人中心菜单
    user_home_user_info_menu = By.LINK_TEXT, '个人信息'
    # 编辑头像
    user_info_edit_head_pic = By.ID, 'preview'
    # 定位iframe
    iframe_loc = By.CSS_SELECTOR, 'div[class="layui-layer-content"] iframe'
    # 上传头像按钮
    iframe_upload_image = By.CSS_SELECTOR, 'div[id="filePicker"] input'
    # 定位上传成功
    upload_success = By.CLASS_NAME, 'success'
    # 点击确定使用
    confirm_use = By.CLASS_NAME, 'saveBtn'
    # 编辑昵称
    edit_nickname = By.ID, 'nickname'
    # 选择性别
    edit_sex = (By.ID, f'{random.choice(["woman", "man", "secret"])}')
    # 编辑生日
    edit_birthday = By.ID, 'birthday'
    birthday = datetime.datetime.now().strftime('%Y-%m-%d')  # 如2023-10-22
    # 点击确认保持按钮
    save_btn = By.CLASS_NAME, 'save'
    # 收货地址菜单
    user_info_address_menu = By.LINK_TEXT, '地址管理'
    # 添加新地址按钮
    add_new_address_btn = By.CSS_SELECTOR, 'a[class="address"] span'
    # 输入收货人姓名
    address_name = By.NAME, 'consignee'
    # 输入收货人手机号
    address_phone = By.NAME, 'mobile'
    # 选择所在地区--省份
    address_province = By.ID, 'province'
    address_province_option = By.XPATH, '//*[@id="province"]/option'
    # 选择所在地区--城市
    address_city_option = By.XPATH, '//*[@id="city"]/option'
    address_city = By.ID, 'city'
    # 选择所在地区--区
    address_district = By.ID, 'district'
    address_district_option = By.CSS_SELECTOR, 'select[id="district"] option'
    # 选择所在地区--街道
    address_twon = By.ID, 'twon'
    address_twon_option = By.CSS_SELECTOR, 'select[id="twon"] option'
    # 输入详细地址
    detail_address = By.NAME, 'address'
    # 输入邮政编码
    postal_code = By.NAME, 'zipcode'
    # 点击保存地址
    save_address_btn = By.ID, 'address_submit'
    # 点击删除地址按钮
    delete_address_btn = By.LINK_TEXT, '删除'
    # 我的购物车按钮
    my_shopping_cart_btn = By.CSS_SELECTOR, 'div[class="c-n fl"] span'
    # 操作成功提示语
    success_tip = By.CSS_SELECTOR, 'div[class="sd1"] span'

    # TODO ****************以下为个人中心页面元素配置信息*******************
    
    # 获取登录用户名信息
    def page_get_user_name(self):
        return self.base_get_text(self.user_home_user_name)

    # 点击返回商城首页按钮
    def page_click_back_home_btn(self):
        self.base_click(self.user_home_back_home_btn)
        self.base_get_allure_image()

    # 点击个人信息菜单
    def page_click_user_info_menu(self):
        self.base_click(self.user_home_user_info_menu)
        self.base_get_allure_image()

    # 编辑头像
    def page_edit_head_pic(self):
        self.base_click(self.user_info_edit_head_pic)
        self.base_get_allure_image()

    # 进入iframe框架
    def page_into_iframe(self):
        self.base_if_frame_exist(self.iframe_loc)
        self.base_get_allure_image()

    # 点击上传图片按钮
    def page_upload_image(self, filepath):
        self.base_find_element(self.iframe_upload_image).send_keys(filepath)
        self.base_get_allure_image()

    # 等待上传完成
    def page_wait_upload_success(self):
        self.base_if_element_exist(self.upload_success)
        self.base_get_allure_image()

    # 点击确定使用
    def page_click_confirm_use(self):
        self.base_click(self.confirm_use)
        self.base_get_allure_image()

    def page_input_edit_nickname(self, nickname):
        self.base_input(self.edit_nickname, nickname)
        self.base_get_allure_image()

    # 编辑性别
    def page_input_edit_sex(self):
        self.base_click(self.edit_sex)
        self.base_get_allure_image()

    # 编辑生日
    def page_input_edit_birthday(self, birthday):
        self.base_input(self.edit_birthday, birthday)
        # 点击昵称输入框来关闭日期插件框
        self.base_click(self.edit_nickname)
        self.base_get_allure_image()

    # 点击确认保存按钮
    def page_click_save_btn(self):
        self.base_click(self.save_btn)
        self.base_get_allure_image()

    # 点击收获地址菜单
    def page_click_address_menu(self):
        self.base_click(self.user_info_address_menu)
        self.base_get_allure_image()

    # 点击增加新地址按钮
    def page_click_add_new_address_btn(self):
        self.base_click(self.add_new_address_btn)
        self.base_get_allure_image()

    # 输入收货人姓名
    def page_input_consignee_name(self, name):
        self.base_input(self.address_name, name)
        self.base_get_allure_image()

    # 输入收货人手机号
    def page_input_consignee_phone(self, phone):
        self.base_input(self.address_phone, phone)
        self.base_get_allure_image()

    # 下拉框选择省
    def page_select_province_debugging(self, province_text):
        self.base_select_by_text(self.address_province, province_text)
        self.base_get_allure_image()

    def page_select_province(self):
        return self.base_new_select(self.address_province, self.address_province_option, "没有省")

    # 下拉框选择市
    def page_select_city(self):
        return self.base_new_select(self.address_city, self.address_city_option, "没有市")

    # 下拉框选择区
    def page_select_district(self):
        return self.base_new_select(self.address_district, self.address_district_option, "没有区")

    # 下拉框选择街道
    def page_select_twon(self):
        return self.base_new_select(self.address_twon, self.address_twon_option, "没有街道")

    # 输入收货人详细地址
    def page_input_detail_address(self, address):
        self.base_input(self.detail_address, address)
        self.base_get_allure_image()

    # 输入收货人邮编
    def page_input_consignee_postcode(self, postcode):
        self.base_input(self.postal_code, postcode)
        self.base_get_allure_image()

    # 点击保存按钮
    def page_click_save_address_btn(self):
        self.base_click(self.save_address_btn)
        self.base_get_allure_image()

    # 点击删除地址按钮
    def page_click_delete_address_btn(self):
        self.base_click(self.delete_address_btn)
        self.base_get_allure_image()

    # 点击我的购物车按钮
    def page_click_my_shopping_cart_btn(self):
        self.base_click(self.my_shopping_cart_btn)
        self.base_get_allure_image()

    # 获取操作成功提示语
    def page_get_success_tip(self):
        return self.base_get_text(self.success_tip)

    # 点击登录账号
    def page_click_login_account(self):
        self.base_click(self.user_home_user_name)
        self.base_get_allure_image()
